
public class Manufacturer extends Thread{

	public static long time = Scarecrow_Factory.time;
	protected static final int PRODUCTION_TIME_MIN = 2000;
	protected static final int PRODUCTION_TIME_MAX = 5000;
	protected static int HALT_TIME = PRODUCTION_TIME_MAX;
	protected static boolean factoryAboutToStopOperation = false;
	protected boolean isSurplus = false;
	
	public void msg(String m) {
	System.out.println("["+(System.currentTimeMillis()-time)+"] "+getName()+": "+m);
	}
	
	public synchronized void setIsSurplus( boolean s ) {
		isSurplus = s;
	}
	
	public synchronized boolean isSurplus() {
		return isSurplus;
	}
	
	public void truckReturns( Truck truck ) {
		if (truck.partIsSurplus() && isSurplus == false){
			setIsSurplus(true);
			msg("Truck #"+truck.truckId+" returned and reported: SURPLUS Halt production");
		}
		else {
			setIsSurplus(false);
			msg("Truck #"+truck.truckId+" returned and reported: CONTINUE production");
		}
		truck.truckReturned = true;		
	}
	
	public synchronized static void factoryIsAboutToStopOperation ( Manufacturer[] manufacturerByNeed ) {
		try {
			manufacturerByNeed[4].join();
			manufacturerByNeed[3].join();
			manufacturerByNeed[2].join();
			manufacturerByNeed[1].join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

	}
	
	public synchronized boolean isFactoryAboutToStopOperation() {
		return factoryAboutToStopOperation;
	}

	public synchronized static void setFactoryAboutToStopOperation(boolean b) {
		factoryAboutToStopOperation = b;
	}
		
	public String toString(){
		return ("Manufacturer "+getName());
	}
}
